package org.grizz.game.view;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.grizz.game.entities.CircularEntity;
import org.grizz.game.entities.LineEntity;
import org.grizz.game.entities.Entity;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.GLCommon;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Rectangle;

public class ScreenTemplate extends AbstractScreen {
	private ShapeRenderer shapeRenderer;
	private List<Entity> entities;
	
	private int screenWidth = Gdx.graphics.getWidth();
	private int screenHeight = Gdx.graphics.getHeight();
	private int areaWidth = screenWidth / 2;
	private int areaHeight = screenHeight / 2;
	
	@Override
	public void show() {
		super.show();
		shapeRenderer = new ShapeRenderer();
		entities = new ArrayList<Entity>();
		
		Random r = new Random();
		
		Rectangle upperLeftCorner = new Rectangle(0, screenHeight / 2, areaWidth, areaHeight);
		Rectangle upperRightCorner = new Rectangle(screenWidth / 2, screenHeight / 2, areaWidth, areaHeight);
		Rectangle lowerLeftCorner = new Rectangle(0, 0, areaWidth, areaHeight);
		Rectangle lowerRightCorner = new Rectangle(screenWidth / 2, 0, areaWidth, areaHeight);
		
		CircularEntity circular1 = new CircularEntity(shapeRenderer, upperRightCorner, 100);
		
		circular1.setColor(new Color(r.nextFloat(), r.nextFloat(), r.nextFloat(), 1));
		
		entities.add(circular1);
		
		for(int i = 0; i < 20; i++) {
			LineEntity line1 = new LineEntity(shapeRenderer, upperLeftCorner);
			LineEntity line2 = new LineEntity(shapeRenderer, lowerRightCorner);
			
			line1.setColor(new Color(r.nextFloat(), r.nextFloat(), r.nextFloat(), 1));
			line2.setColor(new Color(0f, (1f / 20) * i, 0f, 0f));
			
			entities.add(line1);
			entities.add(line2);
			
			CircularEntity backroundCircular = new CircularEntity(shapeRenderer, lowerLeftCorner, 10 * (i+1));
			float greyFactor = 0.3f + (0.2f / 20) * i;
			backroundCircular.setColor(new Color(greyFactor, greyFactor, greyFactor, 0));
			entities.add(backroundCircular);
		}
	}

	@Override
	public void update(float delta) {
		for (Entity entity : entities) {
			entity.update(delta);
		}
	}

	@Override
	public void draw(float delta) {
		GLCommon gl = Gdx.gl;
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
		
		for (Entity entity : entities) {
			entity.draw();
		}
		
		shapeRenderer.setColor(Color.GRAY);
		
		shapeRenderer.begin(ShapeType.Line);
		
		shapeRenderer.line(0, areaHeight, screenWidth, areaHeight);
		shapeRenderer.line(areaWidth, 0, areaWidth, screenHeight);
		
		shapeRenderer.end();
	}

}
